<?php

include_once F_SOURCE . 'user/user.model.class.php';
include_once F_SOURCE . 'group/group.model.class.php';

/**
 * 用户管理CTRL类
 * 
 * @author Dean
 *
 */
class userCtrl extends Ctrl{
	
	/**
	 * 显示用户列表
	 */
	public function show() {
		$view = new View();
		
		$pageIndex = ($_GET['pageIndex']=='' || $_GET['pageIndex']<1)?1:$_GET['pageIndex'];
		
		$userModel = new userModel();
		
		$number = $userModel->countAll();
		
		$userList = $userModel->findAll(($pageIndex-1)*PageNum, PageNum);	
		
		$page = pageCount($number, $pageIndex, '?do=user.show'); 
		
		$data = array(
			'op_select' => array('user'=>"class=select"),
			'userList' => $userList,
			'page' => $page
		);
		
		$view->display($data, 'user_show.html');
	}
	
	/**
	 * 查询单个用户
	 */
	public function info(){
		
		$view = new View();
		
		$id = $_GET['id'];
		
		$userModel = new userModel();
		$userInfo = $userModel->findOne($id);	
		
		$groupModel = new groupModel();
		$groupList = $groupModel->findAll();
		
		$data = array(
			'op' => 'update',
			'op_select' => array('user'=>"class=select"),
			'obj' => $userInfo,
			'groupList' => $groupList
		);
		
		$view->display($data, 'user_view.html');
	}
	
	/**
	 * 跳转新增页面
	 */
	public function view(){
		$view = new View();

		$groupModel = new groupModel();
		$groupList = $groupModel->findAll();
		
		$data = array(
			'op' => $_GET['op'],
			'op_select' => array('user'=>"class=select"),
			'groupList' => $groupList
		);
		
		$view->display($data, 'user_view.html');
	}
	
	/**
	 * 新增用户操作
	 * 
	 */
	public function add(){
		
		$username = $_POST['username'];
		$pwd1 = $_POST['pwd1'];
		$pwd2 = $_POST['pwd2'];
		$email = $_POST['email'];
		$groupID = $_POST['groupID'];
		
		if($pwd1 != $pwd2){
			$this->showMessage('?do=user.view&op=add', 'user_password_diff');
		}
		
		$data = array(
			'username' => $username,
			'pwd' => md5($pwd2),
			'email' => $email,
			'groupID' => $groupID,
			'createTime' => getNowTime(),
			'modifyTime' => getNowTime(),
			'createUser' => $_SESSION['news']['user']->id
		);

		$userModel = new userModel();
		$insertID = $userModel->add($data);
			
		if($insertID > 0){
			$this->showMessage('?do=user.show', 'op_success');
		}else{
			$this->showMessage('?do=user.view&op=add', 'op_failure');
		}
	}
	
	/**
	 * 修改用户信息操作，通过用户编号
	 * 
	 */
	public function modify(){
		
		$id = $_POST['id'];
		$username = $_POST['username'];
		$pwd1 = $_POST['pwd1'];
		$pwd2 = $_POST['pwd2'];
		$email = $_POST['email'];
		$groupID = $_POST['groupID'];
		
		if($pwd1 != $pwd2){
			$this->showMessage('?do=user.view&op=add', 'user_password_diff');
		}
		
		$data = array(
			'username' => $username,
			'email' => $email,
			'groupID' => $groupID,
			'modifyTime' => getNowTime()
		);

		if(!empty($pwd1)){
			$data['pwd']=md5($pwd1);
		}
		
		$where = array(
			'id' => $id
		);
		
		$userModel = new userModel();
		$number = $userModel->modify($data, $where);
			
		if($number > 0){
			$this->showMessage('?do=user.show', 'op_success');
		}else{
			$this->showMessage('?do=user.view&op=add', 'op_failure');
		}
		
	}
	
	/**
	 * 删除用户操作，通过用户编号
	 * 
	 */
	public function delete(){
		
		$id = intval($_GET['id'])>0?intval($_GET['id']):0;
		
		if($id<1){
			$this->showMessage('?do=user.show', 'op_wrong_id');
		}
		
		$data = array(
			'id' => $id
		);
		
		$userModel = new userModel();
		$success = $userModel->delete($data);
		
		if($success > 0){
			$this->showMessage('?do=user.show', 'op_success');
		}else{
			$this->showMessage('?do=user.show', 'op_failure');
		}
	}

}


?>